import java.awt.image.BufferedImage;
import javax.imageio.*;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class ImageProcessing {
	public static int[] readImage(File file)
    {
        BufferedImage image= null;
        try {
            image = ImageIO.read(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        int srcW=image.getWidth();
        int srcH=image.getHeight();
        int[] array=new int[srcW*srcH];
        return array;
    }
	 
	public int[] imgScale(int[] array, int srcW, int srcH, int destW, int destH) {
		int[][][]arrayBefore=new int[srcH][srcW][4];
        for (int i=0;i<srcH;i++)
        {
            int[] row=new int[srcW];
            for (int j=0;j<srcW;j++)
            {
                int counter=i*srcW+j;
                row[j]=array[counter];
            }

            for (int j=0;j<srcW;j++)
            {
                arrayBefore[i][j][0]=(row[j]>>24)&0xFF;
                arrayBefore[i][j][1]=(row[j]>>16)&0xFF;
                arrayBefore[i][j][2]=(row[j]>>8)&0xFF;
                arrayBefore[i][j][3]=(row[j])&0xFF;
            }
        }
        int[][][]arrayAfter=new int[destH][destW][4];

        double rowRatio=((double) srcH)/((double)destH);
        double colRatio=((double)srcW)/((double)destW);

        for(int row=0;row<destH;row++)
        {
            double srcRow=((double)row)*rowRatio;
            double rz=Math.floor(srcRow);
            double rx=srcRow-rz;

            for (int col=0;col<destW;col++)
            {
                double srcCol=((double)col)*colRatio;
                double cz=Math.floor(srcCol);
                double cx=srcCol-cz;

                double c1=(1.0-rx)*(1.0-cx);
                double c2=rx*(1.0-cx);
                double c3=rx*cx;
                double c4=(1.0-rx)*cx;

                arrayAfter[row][col][0]= (int)(c1*arrayBefore[getClip((int)rz,srcH-1,0)][getClip((int)cz,srcW-1,0)][0]+c2*arrayBefore[getClip((int)rz+1,srcH-1,0)][getClip((int)cz,srcW-1,0)][0]+c3*arrayBefore[getClip((int)rz+1,srcH-1,0)][getClip((int)cz+1,srcW-1,0)][0]+c4*arrayBefore[getClip((int)rz,srcH-1,0)][getClip((int)cz+1,srcW-1,0)][0]);
                arrayAfter[row][col][1]= (int)(c1*arrayBefore[getClip((int)rz,srcH-1,0)][getClip((int)cz,srcW-1,0)][1]+c2*arrayBefore[getClip((int)rz+1,srcH-1,0)][getClip((int)cz,srcW-1,0)][1]+c3*arrayBefore[getClip((int)rz+1,srcH-1,0)][getClip((int)cz+1,srcW-1,0)][1]+c4*arrayBefore[getClip((int)rz,srcH-1,0)][getClip((int)cz+1,srcW-1,0)][1]);
                arrayAfter[row][col][2]= (int)(c1*arrayBefore[getClip((int)rz,srcH-1,0)][getClip((int)cz,srcW-1,0)][2]+c2*arrayBefore[getClip((int)rz+1,srcH-1,0)][getClip((int)cz,srcW-1,0)][2]+c3*arrayBefore[getClip((int)rz+1,srcH-1,0)][getClip((int)cz+1,srcW-1,0)][2]+c4*arrayBefore[getClip((int)rz,srcH-1,0)][getClip((int)cz+1,srcW-1,0)][2]);
                arrayAfter[row][col][3]= (int)(c1*arrayBefore[getClip((int)rz,srcH-1,0)][getClip((int)cz,srcW-1,0)][3]+c2*arrayBefore[getClip((int)rz+1,srcH-1,0)][getClip((int)cz,srcW-1,0)][3]+c3*arrayBefore[getClip((int)rz+1,srcH-1,0)][getClip((int)cz+1,srcW-1,0)][3]+c4*arrayBefore[getClip((int)rz,srcH-1,0)][getClip((int)cz+1,srcW-1,0)][3]);
            }
        }

        int arrayEnSh[]=new int[destH*destW*4];
        for(int i=0,c=0;i<destH;i++)
        {
            for (int j=0;j<destW;j++)
            {
                arrayEnSh[c]=((arrayAfter[i][j][0]<<24)&0xFF000000)|((arrayAfter[i][j][1]<<16)&0x00FF0000)|((arrayAfter[i][j][2]<<8)&0x0000FF00)|((arrayAfter[i][j][3])&0x000000FF);
                c++;
            }
        }

        return arrayEnSh;
    }
	public void outImage(int[]array,int srcW,int srcH,String path)
    {
        BufferedImage imageNew=new BufferedImage(srcW,srcH,BufferedImage.TYPE_INT_RGB);
        imageNew.setRGB(0,0,srcW,srcH,array,0,srcW);
        File outFile=new File(path);
        try {
            ImageIO.write(imageNew,"png",outFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private int getClip(int x, int max, int min) {
        return x>max ? max : x<min? min : x;
    }
    	 public static void main(String[] args) {
    		    ImageProcessing image=new ImageProcessing();
    	        File file=new File("src/dengjie.png");
    	        int[]array= image.readImage(file);
    	        int[] arrayEnlarge= image.imgScale(array,960,540,1440,810);
    	        image.outImage(arrayEnlarge,1440,810,"src/largedengjie.png");
    	        int[] arrayShrink= image.imgScale(array,960,540,480,270);
    	        image.outImage(arrayShrink,480,270,"src/ShrinkImage.png");
    	        
    	    }

    	}